package com.goldenautumn.prefabricate.activity.cart

import android.content.Context
import android.view.ViewGroup
import android.widget.LinearLayout
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.entity.GoodsInfoVO
import com.goldenautumn.prefabricate.databinding.ItemCarRecommendedGoodsBinding
import com.goldenautumn.prefabricate.utils.loadImgUrl

class RecommendedGoodsAdapter  : BaseQuickAdapter<GoodsInfoVO, DataBindingHolder<ItemCarRecommendedGoodsBinding>>() {

    override fun onBindViewHolder(holder: DataBindingHolder<ItemCarRecommendedGoodsBinding>, position: Int, item: GoodsInfoVO?) {
        if(item == null) return
        var mBinding  = holder.binding
        var params = LinearLayout.LayoutParams(SizeUtils.dp2px(132f), ViewGroup.LayoutParams.WRAP_CONTENT)
        when (holder.layoutPosition) {
            items.size - 1 -> params.setMargins(SizeUtils.dp2px(16f), 0,SizeUtils.dp2px(16f), 0)
            else -> params.setMargins(SizeUtils.dp2px(16f), 0,0, 0)
        }
        holder.itemView.layoutParams = params

//        mBinding.cover.loadImgUrl(item.goods_image,24)
        mBinding.cover.loadImgUrl("https://ts1.cn.mm.bing.net/th/id/R-C.b0089c802efa0179ae6f1a365473e2ed?rik=lfm%2bJ%2fspw5LX0w&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50079%2f2036.jpg_wh1200.jpg&ehk=qNQXICUl52BVrI2OOpTnCrI4wsTOPGByIeXp5MWc8nw%3d&risl=&pid=ImgRaw&r=0",24)
        mBinding.title.text = "${item.goods_name}"
        mBinding.price.text = (String.format(context.getString(R.string.currency), "${item?.goods_sku?.goods_price}"))
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): DataBindingHolder<ItemCarRecommendedGoodsBinding> {
        return DataBindingHolder(R.layout.item_car_recommended_goods,parent)
    }

    override fun getItemCount(items: List<GoodsInfoVO>): Int {
        return if(items.size > 5) 5 else super.getItemCount(items)
    }

}